package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.yangzihui.*;
import com.cskaoyan.config.zhangshizhi.CountTime;
import com.cskaoyan.service.TopicService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yangzihui
 * @Description 推广管理中的专题管理的controller
 * @Create 2022-06-26 18:20
 * @Version
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    /**
     * 获取专题列表
     *
     * @param baseParam
     * @param subtitle
     * @param title
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:topic:list", "*"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo topicList(BaseParam baseParam, String subtitle, String title) {
        TopicListVO topics = topicService.query(baseParam, subtitle, title);
        return BaseRespVo.ok(topics);
    }

    /**
     * 在修改专题前的回表
     *
     * @param id
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:topic:read", "*"}, logical = Logical.OR)
    @RequestMapping("read")
    public TopicReadVO topicRead(Integer id) {
        if (id == null) {
            TopicReadVO topicReadVO = new TopicReadVO();
            topicReadVO.setErrno(400);
            topicReadVO.setErrmsg("参数错误");
            return topicReadVO;
        }
        DataDTOXX topic = topicService.readTopic(id);
        if (topic == null) {
            TopicReadVO topicReadVO = new TopicReadVO();
            topicReadVO.setErrno(404);
            topicReadVO.setErrmsg("缺少资源");
            return topicReadVO;
        }
        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setData(topic);
        topicReadVO.setErrno(0);
        topicReadVO.setErrmsg("成功");

        return topicReadVO;
    }

    /**
     * 修改专题参数
     *
     * @param topic
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:topic:update", "*"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo topicUpdate(@RequestBody TopicUpdateVO topic) {
        if (StringUtils.isEmpty(topic.getTitle())) {
            return BaseRespVo.invalidParameter("专题标题不能为空");
        }
        if (StringUtils.isEmpty(topic.getSubtitle())) {
            return BaseRespVo.invalidParameter("专题子标题不能为空");
        }
        if (StringUtils.isEmpty(topic.getContent())) {
            return BaseRespVo.invalidParameter("专题内容不能为空");
        }
        if (StringUtils.isEmpty(topic.getPrice().toString())) {
            return BaseRespVo.invalidParameter("商品低价不能为空");
        }

        return topicService.update(topic);
    }

    /**
     * 创建商品
     *
     * @param topicCreateBO
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:topic:create", "*"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo topicCreate(@RequestBody TopicCreateBO topicCreateBO) {
        if (StringUtils.isEmpty(topicCreateBO.getTitle())) {
            return BaseRespVo.invalidParameter("专题标题不能为空");
        }
        if (StringUtils.isEmpty(topicCreateBO.getSubtitle())) {
            return BaseRespVo.invalidParameter("专题子标题不能为空");
        }
        if (StringUtils.isEmpty(topicCreateBO.getContent())) {
            return BaseRespVo.invalidParameter("专题内容不能为空");
        }
        if (StringUtils.isEmpty(topicCreateBO.getPrice().toString())) {
            return BaseRespVo.invalidParameter("商品低价不能为空");
        }
        if (topicService.create(topicCreateBO)) {
            return BaseRespVo.ok(topicCreateBO);
        }
        return BaseRespVo.invalidData();
    }

    /**
     * 删除单个专题
     *
     * @param map
     * @return
     */
    @CountTime
    @RequiresPermissions(value = {"admin:topic:delete", "*"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo topicDelete(@RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        if (id == null) {
            return BaseRespVo.invalidParameter("参数错误");
        }
        if (topicService.delete(id)) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @CountTime
    @RequiresPermissions("*")
    @RequestMapping("batch-delete")
    public BaseRespVo topicBatchDelete(@RequestBody TopicBatchDeleteBO ids) {
        List<Integer> ids1 = ids.getIds();
        if (topicService.batchDelete(ids1)) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

}
